﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Linq;
namespace Exact_Extension
{
    public partial class SpecialDiscountAuthorize : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        int LastRowHandleInvoice = 0;
        public SpecialDiscountAuthorize(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            this.LoadInitializeData();
        }
        public SpecialDiscountAuthorize(ExactDataContext _DS, ISHCMC_Invoice Inv)
        {
            InitializeComponent();
            DS = _DS;
            
                var queryInv = from Invoices in DS.ISHCMC_Invoices
                               where Invoices.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                             
                               && Invoices.ID == Inv.ID && Invoices.SpecialDiscount == true
                               select Invoices;
                if (queryInv.Count() > 0)
                {
                    this.iSHCMCInvoiceBindingSource.DataSource = queryInv;
                }

                else
                {
                    MessageBox.Show("This invoice detail is not special case !!");
                }
        
        }
        private void LoadInitializeData()
        {
            var querySchoolYear = from SCHY in DS.ISHCMC_SchoolYears
                                  select SCHY;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySchoolYear;
            this.lookUpEdit_SchoolYear.ItemIndex = 0;

            this.radioGroup_Authorize.SelectedIndex = 1;
        }
       
        private void LoadData()
        {
            if (radioGroup_Authorize.EditValue.ToString() == "1")
            {
                var queryInv = from Invoices in DS.ISHCMC_Invoices
                               where Invoices.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                               && Invoices.Itemcode == "TF" && Invoices.Authorize.Value == false
                               && Invoices.SpecialDiscount.Value == true
                               select Invoices;
                this.iSHCMCInvoiceBindingSource.DataSource = queryInv;
            }
            else
            {
                var queryInv = from Invoices in DS.ISHCMC_Invoices
                               where Invoices.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                               && Invoices.Itemcode == "TF" && Invoices.Authorize.Value == true
                                 && Invoices.SpecialDiscount.Value == true
                               select Invoices;
                this.iSHCMCInvoiceBindingSource.DataSource = queryInv;
            }
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.LoadData(); 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ISHCMC_Invoice itemInv = (ISHCMC_Invoice)this.gridView_SpecialDiscount.GetRow(this.gridView_SpecialDiscount.GetSelectedRows()[0]);

            ISHCMC_Invoice itemInvoiceUpdate = DS.ISHCMC_Invoices.Single(c => c.ID == itemInv.ID);

            itemInvoiceUpdate.Discount = itemInv.Discount;
            itemInvoiceUpdate.SpecialDiscount = itemInv.SpecialDiscount;
            itemInvoiceUpdate.Authorize = itemInv.Authorize;
            DS.SubmitChanges();
            this.LoadData();

        }

        private void btnAttachment_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_Invoice InvoiceDetail = (ISHCMC_Invoice)this.gridView_SpecialDiscount.GetRow(LastRowHandleInvoice);

            InvoiceAttach InvAttach = new InvoiceAttach(DS, InvoiceDetail);
            InvAttach.Text = InvAttach.Text + " Invoice no :" + InvoiceDetail.ProformaInvoiceNo + " for Student Code : " + InvoiceDetail.StudentID;
            InvAttach.Show();
        }

        private void gridView_SpecialDiscount_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.popupMenu1.ShowPopup(Control.MousePosition);
                LastRowHandleInvoice = e.RowHandle;
            }
        }
    }
}